﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_BCKD06.xaml
    /// </summary>
    public partial class NPC_BCKD06 : UserControl
    {
        #region KhaiBaoBien
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA25";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0200";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc Vinh Phuc";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc Vinh Phuc";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        string strError = "";
        string strThemSuaXoa = "";
        string strTim = "";//"CoDuLieu"; "KhongCoDuLieu"
        string strThongBao = "";
        Int16 p_thang,ifthang;
        Int16 p_nam,ifnam;
        Int16 intDongTrenGrid;
        DataSet ds;
        DataSet dshuynhap;
        DataSet dsbieu05_Entity;
        DataSet dsbieu18_Entity;
        ListItem lstItem;
        DataTable dtHienThi = new DataTable();
        DataTable dtbieu18 = new DataTable();
        #endregion

        //============================================================================================================================
        #region KhoiTaoThongTinKhiLoadForm

        private void setEntity(string str)
        {
            try
            {
                switch (str)
                {
                    case "1": //Loadcombo
                        dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Clear();
                        DataRow dr = dsbieu05_Entity.Tables["D_DVI_QLY"].NewRow();
                        dr["TEN_DVIQLY"] = cboten_donvi.Text.Trim();
                        //break;
                        dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Add(dr);
                        dsbieu05_Entity.AcceptChanges();
                        break;
                    case "2": //Loadgrid
                        dsbieu18_Entity.Tables["PC1_BCKD18"].Rows.Clear();
                        DataRow dr1 = dsbieu18_Entity.Tables["PC1_BCKD18"].NewRow();
                        //dr1["DOI_TUONG"] = " ";
                        //dr1["TS_KH"] = "";
                        //break;
                        dsbieu18_Entity.Tables["PC1_BCKD18"].Rows.Add(dr1);
                        dsbieu18_Entity.AcceptChanges();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void InitCombo_cbo()
        {
            setEntity("1");
            //PC1Business.GlobalServices.Service_NPCUS_D_DVI_QLY GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_D_DVI_QLY();
            DataSet dscobo = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMa_dviqly);
            //DataSet dscobo = GlobalServices.Service_NPC.select_donviqly(dsD_BCKD_Entity);
            dtHienThi = dscobo.Tables[0].Copy();
            string strdonvi = "";
            if (dtHienThi.Rows.Count > 0)
            {
                foreach (DataRow row in dtHienThi.Rows)
                {
                    lstItem = new ListItem();
                    strdonvi = Convert.ToString(row["TEN_DVIQLY"]);
                    //lstItem.v = Convert.ToString(row["TEN_DVIQLY"]);
                    cboten_donvi.Items.Add(strdonvi);
                    cboten_donvi.SelectedIndex = 0;
                }
            }
        }
        private void Initgrid(string ma_donvi)
        {
            setEntity("2");
            if (ma_donvi.ToString().Length == 4)
            {
                //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS1 = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
                DataSet dscobocty = GlobalServices.Service_NPCUS.Chon_cty_dienluc(p_nam, p_thang, ma_donvi, 18);
            }
            //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
            DataSet dscobo = GlobalServices.Service_NPCUS.select_PC1_BCKD18(ma_donvi, p_thang, p_nam);
            dshuynhap = dscobo.Copy();
            dtbieu18 = dscobo.Tables[0].Copy();
            //DataSet dscobo = GlobalServices.Service_NPC.select_donviqly(dsD_BCKD_Entity);
            if (dtbieu18.Rows.Count > 0)
            {
                //DataTable dtTemp = dsD_BCKD_Entity.Tables["PC1_D_BCKD"].Copy();                            
                //dtHienThi = dtTemp;
                //strTim = "CoDuLieu";
                dgrNPC_BCKD06.ItemsSource = dtbieu18.DefaultView;
                // CheckIsEnableButtonSuaXoa();
                dgrNPC_BCKD06.SelectedIndex = 0; ;
            }
        }
        #endregion

        //============================================================================================================================
        #region Function_Process_Data()
        #endregion
        //============================================================================================================================
        public NPC_BCKD06()
        {
            try
            {

                InitializeComponent();
                dsbieu05_Entity = new BCNPCEntities.CMIS2.D_DVI_QLY();
                dtHienThi = dsbieu05_Entity.Tables["D_DVI_QLY"].Clone();
                dsbieu18_Entity = new BCNPCEntities.BCKD15.PC1_D_BCKD_Entity();
                dtbieu18 = dsbieu18_Entity.Tables["PC1_BCKD18"].Clone();
                GetThangNamBC();
                InitCombo_cbo();
                khoitao_bieu18();
                dgrNPC_BCKD06.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void khoitao_bieu18()
        {
            //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
            ds = GlobalServices.Service_NPCUS.KHOITAO_BIEU18(p_nam, p_thang, strMa_dviqly);
            // MessageBox.Show(drv.Row["TS_KH"].ToString());
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            {
                cmdhuy.IsEnabled = false;
                cmdnhap.IsEnabled = false;
                cmdghi.IsEnabled = false;
                GetThangNamBC();
            }
        }

        private void GetThangNamBC()
        {
            try
            {
                GET_THANGNAM();

                if (p_thang.ToString().Length == 1)
                {
                    dtpThangNam.Text = "0" + p_thang.ToString() + "/" + p_nam.ToString();
                }
                else
                {
                    dtpThangNam.Text = p_thang.ToString() + "/" + p_nam.ToString();
                }

                //========      Xac dinh ngay cuoi thang de hien thi vao datetime picker    ========
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        private void GET_THANGNAM()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            p_thang = Convert.ToInt16(dr["THANG"]);
                            p_nam = Convert.ToInt16(dr["NAM"]);
                            ifthang = Convert.ToInt16(dr["THANG"]);
                            ifnam = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                p_thang = Convert.ToInt16(DateTime.Now.Month); ;
                p_nam = Convert.ToInt16(DateTime.Now.Year); ;
                ifthang = Convert.ToInt16(DateTime.Now.Month); ;
                ifnam = Convert.ToInt16(DateTime.Now.Year); ;
            }
        }
        private void cmdin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtHienThi.Rows.Count; i++)
                {
                    if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                    {
                        strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                    }
                }
                //Hien thi con tro bao ban, khi bat dau nhan nut In
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                this.Cursor = Cursors.Wait;
                Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));

                DataSet ds1 = new DataSet();
                setEntity("2");
                //PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18 GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_pc1_bieu18();
                //DataSet dscobo = GlobalServices.Service_NPCUS.select_PC1_BCKD17(strMa_dviqly,p_thang,p_nam);
                DataSet dscobo = GlobalServices.Service_NPCUS.INBIEU(strMa_dviqly, p_nam, p_thang, 18);

                //PC1Business.GlobalServices.Service_NPCUS_PA_TH_IN_BC_SCHT GlobalServices.Service_NPCUS = new PC1Business.GlobalServices.Service_NPCUS_PA_TH_IN_BC_SCHT();
                //ds1 = GlobalServices.Service_NPCUS.THopBCao_SCHT(intNam_pkg, intThang_pkg, strMa_dviqly_pkg, strTuNgay_pkg, strDenNgay_pkg, strMa_KQua_pkg, iCase_pkg, ref strErr);
                //ds_BCKD_QTTH = GlobalServices.Service_NPC.THopBCao_SCHT(intNam_pkg, intThang_pkg, strMa_dviqly_pkg, strTuNgay_pkg, strDenNgay_pkg, strMa_KQua_pkg, iCase_pkg, ref strErr);

                //Goi report de hien thi du lieu tim duoc
                rpt_PC1_BCKD18 crt = new rpt_PC1_BCKD18();
                crt.SetDataSource(dscobo.Tables["PC1_BCKD18"]);
                //UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly);
                UtilReportOMS.RptManager.showReportToWin(this, crt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                //Chuyen che do con tro ve trang thai ban dau
                this.Cursor = null;
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.ToString());
            }
        }

        

        private void cmdhuy_Click(object sender, RoutedEventArgs e)
        {
            cmdchon.IsEnabled = true;
            cmdghi.IsEnabled = false;
            cmdin.IsEnabled = true;
            cmdnhap.IsEnabled = true;
            cmdhuy.IsEnabled = false;
            dgrNPC_BCKD06.IsReadOnly = true;
            dtbieu18 = dshuynhap.Tables[0].Copy();
            if (dtbieu18.Rows.Count > 0)
            {
                dgrNPC_BCKD06.ItemsSource = dtbieu18.DefaultView;
                dgrNPC_BCKD06.SelectedIndex = 1; ;
            }
        }

        private void cmdghi_Click(object sender, RoutedEventArgs e)
        {
          try
           {
            string ma_so;
            Int64 TSV_DXLX, TSV_DXL, TSV_CSCT, DN_TRTH, DN_THHO, DN_BOTH, ST_TRTH, ST_THHO, ST_BOTH, ST_PH, ST_THKH;
            DataRowView drv = dgrNPC_BCKD06.SelectedItem as DataRowView;

            cmdhuy.IsEnabled = false;
            cmdin.IsEnabled = true;
            cmdnhap.IsEnabled = true;
            cmdghi.IsEnabled = false;
            cmdchon.IsEnabled = true;
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
            for (int i = 1; i < 6; i++)
            {
                ma_so = dtbieu18.Rows[i]["MA_SO"].ToString().Trim();
                if (dtbieu18.Rows[i]["TSV_DXLX"].ToString().Trim() == "")
                {
                    TSV_DXLX = 0;
                }
                else
                {
                    TSV_DXLX = Convert.ToInt64(dtbieu18.Rows[i]["TSV_DXLX"].ToString());
                }
                if (dtbieu18.Rows[i]["TSV_DXL"].ToString().Trim() == "")
                {
                    TSV_DXL = 0;
                }
                else
                {
                    TSV_DXL = Convert.ToInt64(dtbieu18.Rows[i]["TSV_DXL"].ToString());
                }
                if (dtbieu18.Rows[i]["TSV_CSCT"].ToString().Trim() == "")
                {
                    TSV_CSCT = 0;
                }
                else
                {
                    TSV_CSCT = Convert.ToInt64(dtbieu18.Rows[i]["TSV_CSCT"].ToString());
                }
                if (dtbieu18.Rows[i]["DN_TRTH"].ToString().Trim() == "")
                {
                    DN_TRTH = 0;
                }
                else
                {
                    DN_TRTH = Convert.ToInt64(dtbieu18.Rows[i]["DN_TRTH"].ToString());
                }
                if (dtbieu18.Rows[i]["DN_THHO"].ToString().Trim() == "")
                {
                    DN_THHO = 0;
                }
                else
                {
                    DN_THHO = Convert.ToInt64(dtbieu18.Rows[i]["DN_THHO"].ToString());
                }
                if (dtbieu18.Rows[i]["DN_BOTH"].ToString().Trim() == "")
                {
                    DN_BOTH = 0;
                }
                else
                {
                    DN_BOTH = Convert.ToInt64(dtbieu18.Rows[i]["DN_BOTH"].ToString());
                }
                if (dtbieu18.Rows[i]["ST_TRTH"].ToString().Trim() == "")
                {
                    ST_TRTH = 0;
                }
                else
                {
                    ST_TRTH = Convert.ToInt64(dtbieu18.Rows[i]["ST_TRTH"].ToString());
                }
                if (dtbieu18.Rows[i]["ST_THHO"].ToString().Trim() == "")
                {
                    ST_THHO = 0;
                }
                else
                {
                    ST_THHO = Convert.ToInt64(dtbieu18.Rows[i]["ST_THHO"].ToString());
                }
                if (dtbieu18.Rows[i]["ST_BOTH"].ToString().Trim() == "")
                {
                    ST_BOTH = 0;
                }
                else
                {
                    ST_BOTH = Convert.ToInt64(dtbieu18.Rows[i]["ST_BOTH"].ToString());
                }
                if (dtbieu18.Rows[i]["ST_PH"].ToString().Trim() == "")
                {
                    ST_PH = 0;
                }
                else
                {
                    ST_PH = Convert.ToInt64(dtbieu18.Rows[i]["ST_PH"].ToString());
                }
                if (dtbieu18.Rows[i]["ST_THKH"].ToString().Trim() == "")
                {
                    ST_THKH = 0;
                }
                else
                {
                    ST_THKH = Convert.ToInt64(dtbieu18.Rows[i]["ST_THKH"].ToString());
                }
                ds = GlobalServices.Service_NPCUS.UPDATE_BIEU18(p_nam, p_thang, strMa_dviqly, ma_so, TSV_DXLX, TSV_DXL, TSV_CSCT, DN_TRTH, DN_THHO, DN_BOTH, ST_TRTH, ST_THHO, ST_BOTH, ST_PH, ST_THKH);

            }
            Initgrid(strMa_dviqly);
            dgrNPC_BCKD06.IsReadOnly = true;
           }
          catch (Exception ex)
          {
              MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
              strTim = "";
              return;
          }
        }

        private void cmdchon_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtHienThi.Rows.Count; i++)
                {
                    if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                    {
                         strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                    }
                }
            // MessageBox.Show(strMa_dviqly);
            khoitao_bieu18();
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

            Initgrid(strMa_dviqly);
            //if (strMa_dviqly.ToString().Length == 4)
            //{
            //    cmdnhap.IsEnabled = false;
            //    cmdhuy.IsEnabled = false;
            //    cmdghi.IsEnabled = false;
            //}
            //else
            //{
                if (p_thang == ifthang && p_nam == ifnam)
                {
                    cmdnhap.IsEnabled = true;
                    cmdhuy.IsEnabled = false;
                    cmdghi.IsEnabled = false;
                }
                else
                {
                    cmdnhap.IsEnabled = false;
                    cmdhuy.IsEnabled = false;
                    cmdghi.IsEnabled = false;
                }
            //}
                dgrNPC_BCKD06.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                strTim = "";
                return;
            }
        }

        private void dtpThangNam_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmdchon.Focus();
            }
        }

        private void cboten_donvi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtpThangNam.Focus();
            }
        }

        private void cmdchon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dgrNPC_BCKD06.Focus();
            }
        }
        private void cmdnhap_Click(object sender, RoutedEventArgs e)
        {
            cmdghi.IsEnabled = true;
            cmdhuy.IsEnabled = true;
            cmdin.IsEnabled = false;
            cmdchon.IsEnabled = false;
            cmdnhap.IsEnabled = false;
            dgrNPC_BCKD06.IsReadOnly = false;
            dgrNPC_BCKD06.Focus();
            dgrNPC_BCKD06.SelectedIndex = 1;
        }

        private void cboten_donvi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}